package com.swn.common.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

public class ExpressQueryUtil {
	public final static String host = "http://jisukdcx.market.alicloudapi.com";
	public static final String type = "auto";  //物流公司类型，默认为自动匹配

	/**
	 * 根据运单编号查询物流信息
	 * @param number
	 * @throws Exception void
	 * @Author 王万兴
	 * @Date 2017年9月11日 下午5:08:29 
	 * @Note <这里是方法的备注>
	 * @company 济南舒卫能生物技术有限公司
	 */
	public static Map<String, Object> queryExpress(String number) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		String path = "/express/query";
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Authorization", "APPCODE 50afea8f465a43dfadea04339d5ded68");
		// 请求的query
		Map<String, String> querys = new HashMap<String, String>();
		querys.put("number", number);
		querys.put("type", type);
		try {
			HttpResponse response = HttpUtils.doGet(host, path, "GET", headers, querys);
			String result = EntityUtils.toString(response.getEntity());
			if(null != result && !"".equals(result)){
				JSONObject json = JSONObject.fromObject(result);
				if (json.getInt("status") != 0) {
					map.put("failReson", json.getString("msg"));
				} else {
					JSONObject resultarr = json.optJSONObject("result");
					if (resultarr != null) {
						if (resultarr.opt("list") != null) {
							JSONArray list = resultarr.optJSONArray("list");
							List<Map<String, Object>> mapList = new ArrayList<Map<String,Object>>();
							for (int j = 0; j < list.size(); j++) {
								JSONObject list_obj = (JSONObject) list.opt(j);
								if (list_obj != null) {
									String time = list_obj.getString("time");
									String status = list_obj.getString("status");
									Map<String, Object> paramMap = new HashMap<String, Object>();
									paramMap.put("time", time);
									paramMap.put("status", status);
									mapList.add(paramMap);
								}
							}
							map.put("wuliuList", mapList);
						}
					}
					map.put("issign", resultarr.getInt("issign"));//1代表已签收  
				}
				map.put("status",json.getInt("status"));//0代表成功 
			}else{
				map.put("status","1");//0代表成功 
				map.put("failReson","未查到物流信息");//0代表成功 
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
}
